import { createRouter, createWebHashHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'


import { appRoutes } from './routes/index.js';

const routes = [
  {
    path: '/chat',
    component: MainLayout,
    children: [
      ...appRoutes,
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/HomePage.vue')
      },
      {
        path: 'qa',
        name: 'qa',
        component: () => import('@/views/QAPage.vue')
      },
      {
        path: 'about',
        name: 'about',
        component: () => import('@/views/AboutPage.vue')
      },
      {
        path: 'recharge',
        name: 'recharge',
        component: () => import('@/views/personal/RechargeView.vue')
      },
      {
        path: 'payment-result',
        name: 'payment-result',
        component: () => import('@/views/payment/PaymentResult.vue')
      }
    ]
  },
  // 首页内容
  {
    path: '/',
    component: () => import('@/views/index.vue'),
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('@/views/news.vue')
  },
  {
    path: '/admin/login',
    name: 'admin-login',
    component: () => import('@/views/admin/login.vue')
  },
  {
    path: '/admin',
    component: () => import('@/layouts/AdminLayout.vue'),
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'admin-dashboard',
        component: () => import('@/views/admin/dashboard/index.vue')
      },
      // 症状汤药管理路由
      {
        path: 'symptom-category',
        name: 'admin-symptom-category',
        component: () => import('@/views/admin/symptom-category/index.vue'),
        meta: { title: '症状分类管理' }
      },
      {
        path: 'symptom',
        name: 'admin-symptom',
        component: () => import('@/views/admin/symptom/index.vue'),
        meta: { title: '症状管理' }
      },
      {
        path: 'formula',
        name: 'admin-formula',
        component: () => import('@/views/admin/formula/index.vue'),
        meta: { title: '汤药管理' }
      },
      {
        path: 'data-sync',
        name: 'admin-data-sync',
        component: () => import('@/views/admin/DataSync.vue'),
        meta: { title: '数据同步管理' }
      },
      // {
      //   path: 'users',
      //   name: 'admin-users',
      //   component: () => import('@/views/admin/users/index.vue')
      // },
      // {
      //   path: 'users/roles',
      //   name: 'admin-user-roles',
      //   component: () => import('@/views/admin/users/roles.vue')
      // },
      // {
      //   path: 'docs-latest',
      //   name: 'admin-docs-latest',
      //   component: () => import('@/views/admin/content/docs-latest.vue')
      // },
      // {
      //   path: 'docs-works',
      //   name: 'admin-docs-works',
      //   component: () => import('@/views/admin/content/docs-works.vue')
      // },
      // {
      //   path: 'docs-emotion',
      //   name: 'admin-docs-emotion',
      //   component: () => import('@/views/admin/content/docs-emotion.vue')
      // },
      // {
      //   path: 'health-consultation',
      //   name: 'admin-health-consultation',
      //   component: () => import('@/views/admin/health/consultation.vue')
      // },
      // {
      //   path: 'health-records',
      //   name: 'admin-health-records',
      //   component: () => import('@/views/admin/health/records.vue')
      // },
      // {
      //   path: 'orders',
      //   name: 'admin-orders',
      //   component: () => import('@/views/admin/finance/orders.vue')
      // },
      // {
      //   path: 'payments',
      //   name: 'admin-payments',
      //   component: () => import('@/views/admin/finance/payments.vue')
      // },
      // {
      //   path: 'combos',
      //   name: 'admin-combos',
      //   component: () => import('@/views/admin/finance/combos.vue')
      // },
      {
        path: 'settings',
        name: 'admin-settings',
        component: () => import('@/views/admin/settings/index.vue')
      }
    ],
  }
]

const router = createRouter({
  history: createWebHashHistory('/'),
  routes
})

// const authRequiredRoutes = [
//   '/'
// ];


// 全局前置守卫
router.beforeEach((to, from, next) => {
  // // 在路由外初始化store
  // const userStore = useUserStore();
  // const modalStore = useModalStore();
  
  // // 检查用户是否已登录
  // const isLoggedIn = userStore.getLoginStatus;
  
  // // 检查目标路由是否需要登录
  // const requiresAuth = !authRequiredRoutes.includes(to.path);
  
  // // 如果路由需要登录但用户未登录
  // if (requiresAuth && !isLoggedIn) {
  //   // 打开登录弹窗并记录重定向路径
  //   modalStore.openLoginModal(to.fullPath);
  //   // 重定向到首页
  //   next('/');
  //   return;
  // }
  
  // 正常导航
  next();
});

export default router
